
import lifecycle from '/util/lifecycle';
import animModal from '/util/items';

const app = getApp();

Page({
  data: {
    sjArray: [],
    fzrArray:[],
    zxztArray:[],
    sjidArray:[],
    khArray:[],
    khIndex:0,
    sjIndex: 0,
    fzrIndex: 0,
    zxztIndex: 0,
    reportList:{},
    isupdate:0,  //0新增，1修改
    rId :'',
    maxsjid:'',
    sjid:'',
    //wth
    currentData:'',
    weekId:'',
    followMan: [],
    mid:'',
    departmentid:'',
    fm:'',
    fi:'',
    followManIndex:'',
    state:'',
  },
  onLoad: function (options) {
    var that = this;
    var url = app.globalData.url;
    var id = options.rId;
    //wth
    var fm = '';
    var fi = '';

    this.setData({
      rId:id,
      sjid:app.globalData.sjId
    });
    if(id!=''&&id!=null&&id!=undefined){
      dd.httpRequest({ 
            url:url+'/api/BusinessReport/selBusinessReportByRid',
            method:'post',
            datatype:'json',
            data:{
              rId:id,
            },
            success:function(res){
              console.log('update');
              that.setData({
                reportList:res.data.data,
                sjIndex:res.data.data.rType,
                zxztIndex:res.data.data.rZt,
                isupdate:'1',
                fm:res.data.data.followMan,//wth
                state:res.data.data.state,
              });
              that.selDitByTypeByfzr();
              that.selDitByTypeBytype();
              that.selDitByTypeByzxzt();
              that.selKh(res.data.data.khName);
            } 
       });

        //wth
        var currentData='';
        var goal ='';
        var result ='';
        var plan ='';
        var weekId  = '';
        dd.httpRequest({
          url: url+'/api/hr/weekly/yearMonth',
          method: 'POST',
          data: {
          },
          dataType: 'json',
          success: function(res) {
            currentData=res.data,
              dd.httpRequest({
                  url: url+'/api/hr/weekly/list',
                  method: 'POST',
                  data: {
                    rid:id,
                    followData:currentData,
                  },
                  dataType: 'json',
                  success: function(res) {
                      res.data.data.forEach(function(index){
                          weekId = index.id;
                          goal = index.goal;
                          result = index.result;
                          plan = index.plan;
                      })
                      that.data.followMan.forEach(function(value,index){
                            if(value==that.data.fm){
                                fi=index;
                                var thats = that;
                                dd.httpRequest({ 
                                  url:url+'/api/hr/weekly/followMan',
                                  method:'post',
                                  datatype:'json',
                                  data:{
                                    followMan:fm,
                                  },
                                  success:function(res){
                                    res.data.data.forEach(function(index){
                                        thats.setData({
                                          mid:index.userId,
                                          departmentid:index.deptId,
                                          followManIndex:fi,
                                        });
                                    })
                                  } 
                              });
                            }
                          })
                      if(that.data.state=='3'){
                        that.setData({
                            weekId:weekId,
                            currentData:currentData,
                            goal: goal,
                            result:result,
                            plan:plan,
                            flag:"edit",
                        });
                      }
                  },
                  fail: function(res) {
                    dd.alert({content: 'fail'});
                  },
                  complete: function(res) {
                    // dd.alert({content: 'complete'});
                  }
              });
          },
          fail: function(res) {
            dd.alert({content: 'fail'});
          },
          complete: function(res) {
            // dd.alert({content: 'complete'});
          }
        });

    }else{
      that.selDitByTypeByfzr();
      that.selDitByTypeBytype();
      that.selDitByTypeByzxzt();
      that.selKh();

      //wth
      dd.httpRequest({ 
        url:url+'/api/hr/weekly/followMan',
        method:'post',
        datatype:'json',
        data:{
          userId:app.globalData.userinfo.userid,
        },
        success:function(res){
          res.data.data.forEach(function(index){
                  fm = index.userName;
                  dd.httpRequest({ 
                      url:url+'/api/hr/weekly/followMan',
                      method:'post',
                      datatype:'json',
                      data:{
                      },
                      success:function(res){
                        res.data.data.forEach(function(value,index){
                            if(value.userName==fm){
                                  fi=index;
                                    that.setData({
                                        followManIndex:fi,
                                    });
                              }
                        })
                      } 
                  });
              })
        } 
    });
    }
  },
  onSubmit(e) {
    var that = this;
    var url = app.globalData.url;
    var department=app.globalData.userinfo.department;    //部门id
    var khIndex = this.data.khIndex;
    var kh_name =this.data.khArray[khIndex].gsid;
    var r_name =e.detail.value.r_name;
    var r_id =e.detail.value.r_id;
    var r_money =e.detail.value.r_money;
    var r_type =this.data.sjIndex;
    var r_tb_time =e.detail.value.r_tb_time;
    var r_yh_jcl =e.detail.value.r_yh_jcl;
    var fzrIndex = this.data.fzrIndex;
    var r_fzr =this.data.fzrArray[fzrIndex].userId;
    var r_zt =this.data.zxztIndex;
    var r_ms=e.detail.value.r_ms;
    var c_uid =app.globalData.userinfo.userid;
    //wth
    var followMan =e.detail.value.followMan;
    if(kh_name==''){
       dd.showToast({
          content: '请完善商机客户名称！', // 文字内容
        });
        return;
    }
     if(r_name==''){
       dd.showToast({
          content: '请完善商机项目名称！', // 文字内容
        });
        return;
    }
     if(r_id==''){
       dd.showToast({
          content: '请完善商机编号！', // 文字内容
        });
        return;
    }
   
     if(that.data.isupdate==1){
       if(r_id!=that.data.rId){
        dd.showToast({
            content: '商机编号不允许修改！', // 文字内容
          });
          that.setData({
            'reportList.rId':that.data.rId,
          })
          return;
      }
     }
    if(r_money==''){
        dd.showToast({
            content: '请完善商机预算金额！', // 文字内容
          });
          return;
      }
     if(r_yh_jcl==''){
       dd.showToast({
          content: '请完善商机决策链信息！', // 文字内容
        });
        return;
    }
     if(r_ms==''){
       dd.showToast({
          content: '请完善商机概述信息！', // 文字内容
        });
        return;
    }
    if(followMan==''){
      dd.showToast({
          content: '请选择跟进人！',
        });
        return;
      }

    //wth
    var goal =e.detail.value.goal;
    var result =e.detail.value.result;
    var plan =e.detail.value.plan;
    var followMan =e.detail.value.followMan;
   if(that.data.isupdate==0){
      dd.httpRequest({ 
            url:url+'/api/BusinessReport/insetReport',
            method:'post',
            datatype:'json',
            data:{
              khName: kh_name,
              rName: r_name,
              departmentId:department,
              rId: r_id,
              rMoney: r_money,
              rType: r_type,
              rTbTime: r_tb_time,
              rYhJcl: r_yh_jcl,
              rFzr: r_fzr,
              rZt: r_zt,
              rMs:r_ms,
              cUid: c_uid,//wth
              followMan:followMan,
              followUid:that.data.mid,
              followDid:that.data.departmentid,
              followData:that.data.currentData,
            },
            success:function(res){
              if(res.data.data==1){
                dd.showToast({
                      content: '提交成功', // 文字内容
                });
                dd.redirectTo({
                  url: '../summary/summary',
                })
              }else{
                 dd.showToast({
                      content: '提交失败', // 文字内容
                });
                dd.redirectTo({
                  url: '../summary/summary'
                })
              }
            } 
      });
   }else{
     dd.httpRequest({ 
            url:url+'/api/BusinessReport/updateReport',
            method:'post',
            datatype:'json',
            data:{
              khName: kh_name,
              rName: r_name,
              rId: that.data.rId,
              rMoney: r_money,
              rType: r_type,
              rTbTime: r_tb_time,
              rYhJcl: r_yh_jcl,
              rFzr: r_fzr,
              rZt: r_zt,
              rMs:r_ms,
              uUid: c_uid,
              followMan:followMan,//wth
              followUid:that.data.mid,
              followDid:that.data.departmentid,
              followData:that.data.currentData,
            },
            success:function(res){
              if(res.data.data==1){
                if((that.data.weekId==''&& goal!='')||(that.data.weekId==''&& result!='')||(that.data.weekId==''&& plan!='')){
                    if(goal==''){
                      dd.showToast({
                        content: '请完善客户拜访目的！', 
                      });
                      return;
                  }
                    if(result==''){
                      dd.showToast({
                        content: '请完善达成意向结果！', 
                      });
                      return;
                  }
                    if(plan==''){
                      dd.showToast({
                        content: '请完善推进计划！',
                      });
                      return;
                  }
                 
                    dd.httpRequest({
                        url: url+'/api/hr/weekly/add',
                        method: 'POST',
                        data: {
                          goal:goal,
                          result:result,
                          plan:plan,
                          rid:r_id,
                          followData:that.data.currentData,
                          followMan:followMan,
                          mid:that.data.mid,
                          departmentid:that.data.departmentid,
                          createMan:r_fzr,
                          createUid:c_uid,
                          createDid:department,
                        },
                        dataType: 'json',
                        success: function(res) {
                           dd.showToast({
                            content: '提交成功',
                            });
                            dd.redirectTo({
                                url: '../summary/summary',
                              })
                        },
                        fail: function(res) {
                          dd.alert({content: 'fail'});
                        },
                        complete: function(res) {
                          // dd.alert({content: 'complete'});
                        }
                    });
                }else if(that.data.weekId!=''){
                  if(goal==''){
                      dd.showToast({
                        content: '请完善客户拜访目的！', 
                      });
                      return;
                  }
                    if(result==''){
                      dd.showToast({
                        content: '请完善达成意向结果！', 
                      });
                      return;
                  }
                    if(plan==''){
                      dd.showToast({
                        content: '请完善推进计划！',
                      });
                      return;
                  }
                  
                    dd.httpRequest({
                      url: url+'/api/hr/weekly/edit',
                      method: 'POST',
                      data: {
                        id:that.data.weekId,
                        goal:goal,
                        result:result,
                        plan:plan,
                        rid:r_id,
                        followMan:followMan,
                        mid:that.data.mid,
                        departmentid:that.data.departmentid,
                        createMan:r_fzr,
                        createUid:c_uid,
                        createDid:department,
                      },
                      dataType: 'json',
                      success: function(res) {
                        dd.showToast({
                          content: '修改成功',
                          });
                          dd.redirectTo({
                            url: '../summary/summary',
                          })
                        },
                        fail: function(res) {
                          dd.alert({content: 'fail'});
                        },
                        complete: function(res) {
                          // dd.alert({content: 'complete'});
                        }
                      });
                }else if(that.data.weekId==''&& goal==''&& result==''&& plan==''){
                    dd.showToast({
                      content: '修改成功',
                      });
                      dd.redirectTo({
                        url: '../summary/summary'
                      })
                }
                
              }else{
                 dd.showToast({
                      content: '修改失败', // 文字内容
                });
                dd.redirectTo({
                  url: '../summary/summary',
                })
              }
            } 
      });
   }
  },
  onReset(e) {
   dd.showToast({
          content: '重置成功', // 文字内容
    });
  },
  bindObjPickerChangesj(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      sjIndex: e.detail.value,
    });
  },
   bindObjPickerChangefzr(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      fzrIndex: e.detail.value,
    });
  },
   bindObjPickerChangezxzt(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      zxztIndex: e.detail.value,
    });
  },
   bindObjPickerChangekh(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      khIndex: e.detail.value,
    });
  },
  selDitByTypeBytype(){
    
    var that =this;
    var url = app.globalData.url;
    dd.httpRequest({ 
            url:url+'/api/BusinessReport/selDitByType',
            method:'post',
            datatype:'json',
            data:{
              dict_type:'r_type',
            },
            success:function(res){
              that.setData({
                sjArray:res.data.data,
              });
            } 
    });
  },
  selDitByTypeByzxzt(){
    
    var that =this;
    var url = app.globalData.url;
    dd.httpRequest({ 
            url:url+'/api/BusinessReport/selDitByType',
            method:'post',
            datatype:'json',
            data:{
              dict_type:'r_zxzt',
            },
            success:function(res){
              that.setData({
                zxztArray:res.data.data,
              });
            } 
    });
  },
  selKh(khName){
    var that =this;
    var url = app.globalData.url;
    dd.httpRequest({ 
            url:url+'/api/BusinessReport/selectKehuInfoList',
            method:'post',
            datatype:'json',
            data:{
            },
            success:function(res){
              that.setData({
                khArray:res.data.data,
              });
              res.data.data.forEach(function (item,index){
                if(khName==item.gsid){
                  that.setData({
                    khIndex:index,
                  })
                }
              })
            } 
    });
  },
  selDitByTypeByMaxsjid(){
    var that =this;
    var url = app.globalData.url;
    var sjid=app.globalData.userinfo.sjId;
    dd.httpRequest({ 
            url:url+'/api/BusinessReport/selMaxSjID',
            method:'post',
            datatype:'json',
            data:{
              cUid:app.globalData.userinfo.userid,
            },
            success:function(res){
              that.setData({
                maxsjid:res.data.data,
                    'reportList.rId':sjid+'-'+res.data.data,
              });
               console.log('maxsj');
            } 
    });
  },
   selDitByTypeByfzr(){
    var that =this;
    var url = app.globalData.url;
      dd.httpRequest({ 
            url:url+'/api/BusinessReport/selfzr',
            method:'post',
            datatype:'json',
            data:{
            },
            success:function(res){ 
               console.log('fzr');
              that.setData({
                fzrArray:res.data.data,
              });
              if(that.data.isupdate==0){
                res.data.data.forEach(function (item,index){
                  if(item.userName==app.globalData.userinfo.name){
                      that.setData({
                          fzrIndex:index,
                      });
                  }
                });
              that.selDitByTypeByMaxsjid();
              }
            } 
    });
   },
  onShow() {
    var that =this;
    var url = app.globalData.url;
    var followMan = []; 
    dd.httpRequest({ 
        url:url+'/api/hr/weekly/followMan',
        method:'post',
        datatype:'json',
        data:{},
        success:function(res){
          res.data.data.forEach(function(index){
              followMan.push(index.userName);
          })
          that.setData({
            followMan:followMan,
          });
        } 
    });
  },
  followManChange(e) {
    var that =this;
    var url = app.globalData.url;
    dd.httpRequest({ 
        url:url+'/api/hr/weekly/followMan',
        method:'post',
        datatype:'json',
        data:{
          followMan:that.data.followMan[e.detail.value],
        },
        success:function(res){
          res.data.data.forEach(function(index){
              that.setData({
                mid:index.userId,
                departmentid:index.deptId
              });
          })
        } 
    });
    this.setData({
      followManIndex: e.detail.value,
    });
  },

  weekHistory:function(e){
    var rId = e.currentTarget.dataset.rId;
    dd.navigateTo({
        url: '../weekly/history/history?rid='+rId,
      })
  },
  
});
